//
//  FTQPersonalController.m
//  xiangmu
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 rzt. All rights reserved.
//

#import "FTQPersonalController.h"
#import "PwdViewController.h"
#import "ZhiFuViewController.h"

@interface FTQPersonalController (){
    NSMutableArray *_selectedPhotos;
}
@property(strong,nonatomic)NSMutableArray* dataArr;
@property(strong,nonatomic)NSMutableDictionary* dataDic;
@end

@implementation FTQPersonalController

-(void)viewWillAppear:(BOOL)animated{
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
    
    //头像赋值
    if (self.dataArr[self.number][@"headimg"]==nil) {
        self.headimgview.image=[UIImage imageNamed:@"默认头像"];
    }else{
        UIImage *image = [UIImage imageWithData: self.dataArr[self.number][@"headimg"]];
        self.headimgview.image=image;
    }
    //账号
    self.username.text=self.dataArr[self.number][@"username"];
    //昵称
    if (self.dataArr[self.number][@"nickname"]==nil) {
        self.nicknameLabel.text=self.dataArr[self.number][@"username"];
    }else{
        self.nicknameLabel.text=self.dataArr[self.number][@"nickname"];
    }
    //支付密码
    if ([self.dataArr[self.number][@"zhifupwd"] isEqualToString:@"无"]) {
        self.zhifupwd.text=@"设置支付密码";
    }else{
        self.zhifupwd.text=@"修改支付密码";
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.backView.hidden=YES;
    self.title=@"账户设置";
    self.dataArr=[[NSMutableArray alloc] init];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
    
    self.dataDic=[[NSMutableDictionary alloc] init];
    self.dataDic=[NSMutableDictionary dictionaryWithDictionary:self.dataArr[self.number]];
    //头像赋值
    if (self.dataArr[self.number][@"headimg"]==nil) {
        self.headimgview.image=[UIImage imageNamed:@"默认头像"];
    }else{
        UIImage *image = [UIImage imageWithData: self.dataArr[self.number][@"headimg"]];
        self.headimgview.image=image;
    }
    //账号
    self.username.text=self.dataArr[self.number][@"username"];
    //昵称
    if (self.dataArr[self.number][@"nickname"]==nil) {
        self.nicknameLabel.text=self.dataArr[self.number][@"username"];
    }else{
        self.nicknameLabel.text=self.dataArr[self.number][@"nickname"];
    }
    //支付密码
    if ([self.dataArr[self.number][@"zhifupwd"] isEqualToString:@"无"]) {
        self.zhifupwd.text=@"设置支付密码";
    }else{
        self.zhifupwd.text=@"修改支付密码";
    }
    //保存按钮
    UIButton *rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [rightBtn addTarget:self action:@selector(rightBtnTap) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    UIBarButtonItem *rightitem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem=rightitem;
}
-(void)rightBtnTap{
    [self.dataDic setObject:self.nicknameLabel.text forKey:@"nickname"];
    NSData *imageData = UIImageJPEGRepresentation(self->_selectedPhotos[0],0.1);
    if (imageData==nil) {
       
    }else{
         [self.dataDic  setObject:imageData forKey:@"headimg"];
    }
    [self.dataArr removeObjectAtIndex:self.number];
    [self.dataArr insertObject:self.dataDic atIndex:self.number];
    kUserDefaultSetObjectForKey(self.dataArr, @"user");
    MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.completionBlock = ^(){
        [self.view makeToast:@"保存成功~"
                    duration:2.0
                    position:CSToastPositionCenter];
    };
    [HUD hideAnimated:YES afterDelay:1.0];

}

- (IBAction)changePwd:(id)sender {
    if ([self.dataDic[@"password"] isEqualToString:@"我是游客"]) {
        [self.view makeToast:@"游客登录无法修改密码~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else{
        PwdViewController* change=[[PwdViewController alloc] init];
        [self.navigationController pushViewController:change animated:YES];
    }

}

- (IBAction)outBtnTap:(id)sender {
    self.backView.hidden=NO;
}

- (IBAction)headimgBtnTap:(id)sender {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVc.allowPickingVideo=NO;
    imagePickerVc.allowTakeVideo=NO;
    imagePickerVc.naviBgColor=rgba(255, 78, 19, 1);
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        self->_selectedPhotos = [NSMutableArray arrayWithArray:photos];
        [self.headimgview setImage:self->_selectedPhotos[0]];
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
- (IBAction)backviewSureTap:(id)sender {
    self.backView.hidden=YES;
    if ([self.dataDic[@"username"] isEqualToString:@"Tourist"]) {
        [self.dataArr removeObjectAtIndex:0];
        kUserDefaultSetObjectForKey(self.dataArr, @"user");
    }
    kUserDefaultSetObjectForKey(nil, @"number");
    kUserDefaultSetObjectForKey(nil,@"userid");
    [self.navigationController popViewControllerAnimated:YES];
    self.block(@"nologin");
}

- (IBAction)backviewCancelTap:(id)sender {
    self.backView.hidden=YES;
}
- (IBAction)zhifuPwdBtn:(id)sender {
    if ([self.dataDic[@"password"] isEqualToString:@"我是游客"]) {
        [self.view makeToast:@"游客登录无法支付密码~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else{
    ZhiFuViewController* zhifu=[[ZhiFuViewController alloc] init];
    [self.navigationController pushViewController:zhifu animated:YES];
    }
}
@end
